#include <mpi.h>
#include<stdio.h>
static long num_steps=1000000000;
double step;
// #define numprocs 4
void main(int argc,char* argv[]){
    int i,myid,numprocs;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Status status;
    double x,pi=0.0,sum[numprocs];
    double start_time,end_time;
    step=1.0/(double)num_steps;
    start_time=MPI_Wtime();
    if(myid!=0){
        for(i=myid,sum[myid]=0.0;i<num_steps;i=i+numprocs-1){
            x=(i-0.5)*step;
            sum[myid]+=4.0/(1.0+x*x);
        }
        MPI_Send(&sum[myid],1,MPI_DOUBLE,0,99,MPI_COMM_WORLD);
    }else{
        for(i=1;i<numprocs;i++){
            MPI_Recv(&sum[i],1,MPI_DOUBLE,i,99,MPI_COMM_WORLD,&status);
            pi+=sum[i]*step;
            }
        printf("Pi的值为：%.20f\n",pi);
        end_time=MPI_Wtime();
        printf("程序运行时间：%f\n",end_time-start_time);
    }
    MPI_Finalize();
}